package socket2;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class Server1 {
    /**
     * 声明服务器端套接字
     */

    private ServerSocket serverSocket;
    private List<PrintWriter> allOut = new ArrayList<>();

    /**
     * 构造方法
     */
    public Server1(){
        try {
            System.out.println("正在启动服务器端");
            serverSocket = new ServerSocket(8088);
            System.out.println("服务端启动完毕");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void start(){
        try {

            while (true) {
                System.out.println("等待客户端连接");
                Socket socket = serverSocket.accept();

                //启动线程处理
                ClientHandle handle = new ClientHandle(socket);
                Thread t = new Thread(handle);
                t.setName(socket.getInetAddress().toString());
                System.out.println("一个客户端连接了"+t.getName());
                t.start();

            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Server1 server = new Server1();
        server.start();
    }
    private class ClientHandle implements Runnable{
        private Socket socket;
        private String host;
        public ClientHandle(Socket socket){
            this.socket = socket;
            host = socket.getInetAddress().getHostAddress();
        }
        public void run(){
            PrintWriter pw = null;
            try {
                InputStream in = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(in, StandardCharsets.UTF_8);
                BufferedReader br = new BufferedReader(isr);

                //通过socket获取输出流用来将消息发送给远端计算机(客户端)
                OutputStream out = socket.getOutputStream();
                OutputStreamWriter osw
                        = new OutputStreamWriter(out,StandardCharsets.UTF_8);
                BufferedWriter bw = new BufferedWriter(osw);
                pw = new PrintWriter(bw,true);
                //将该输出流存入共享集合

                synchronized (allOut) {
                    allOut.add(pw);
                }

                sendMessage(host+"上线了，当前人数:"+allOut.size());

                String message;
                while((message = br.readLine())!=null){
                    System.out.println(host+"说： "+message);
                    sendMessage(host+"说"+message);
                    try {
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            } catch (IOException e) {
            }finally {

                synchronized (allOut) {
                    allOut.remove(pw);
                    sendMessage(host+"下线了,当前人数"+allOut.size());
                }

            }
        }

        private void sendMessage(String message){
            synchronized (allOut) {
                for(PrintWriter o : allOut) {
                    o.println(message);
                }
            }
        }
    }

}
